#pragma once
#include "DataRequest.hpp"

class UsersHandler : public DataRequest<UsersHandler>
{
public:
	UsersHandler() :
		m_gid(-1)
	{
		std::string			gid;

		if(request::get("gid", gid))
		{
			m_gid = atoi(gid.c_str());
		}

		request::get("s", m_search);
	}

public:
	bool get_count_sql(sql_string<>& sql)
	{
		if(-1 == m_gid)
		{
			return false;
		}

		if(0 == m_search.length())
		{
			sql.format("select count(*) from t_users where gid=%d",
					m_gid);
		}
		else
		{
			sql.format("select count(*) from t_users"
					" where gid=%d and (name like '%%%q%%'"
					" or email like '%%%q%%' or phone like '%%%q%%'"
					" or qq like '%%%q%%' or msn like '%%%q%%')",
					m_gid, m_search.c_str(),
					m_search.c_str(), m_search.c_str(),
					m_search.c_str(), m_search.c_str());
		}
		return true;
	}

	bool get_data_sql(sql_string<>& sql)
	{
		if(-1 == m_gid)
		{
			return false;
		}

		if(0 == m_search.length())
		{
			sql.format("select uid,name,email,phone,qq,msn from t_users"
					" where gid=%d order by uid",
					m_gid);
		}
		else
		{
			sql.format("select uid,name,email,phone,qq,msn from t_users"
					" where gid=%d and (name like '%%%q%%'"
					" or email like '%%%q%%' or phone like '%%%q%%'"
					" or qq like '%%%q%%' or msn like '%%%q%%')"
					" order by uid",
					m_gid, m_search.c_str(),
					m_search.c_str(), m_search.c_str(),
					m_search.c_str(), m_search.c_str());
		}

		return true;
	}

private:
	int			m_gid;
	std::string m_search;
};

